package net.sureon.web.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.app.UserSession;
import net.sureon.biz.CompanyConfigBO;
import net.sureon.biz.CompanyMgr;
import net.sureon.common.SureonConstants;
import net.sureon.common.exception.SureonException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 
 * @author Administrator 将路径信息保存到数据库中，
 */
public class SystemSet implements Controller {

	private static final Log logger = LogFactory.getLog(SystemSet.class);
	private CompanyMgr companyMgr;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		long companyID = ((net.sureon.app.UserSession) request.getSession()
				.getAttribute(UserSession.HTTP_SESSION_KEY)).getCompanyID();

		String isFirstGo = request.getParameter("isFirstGo");
		if ("false".equals(isFirstGo)) {
			// // 表示提交网页左上角logo的表单，处理更换网页左上角logo的操作

			// 网页左上角图标链接地址
			String linkForLeftUpLogo = request
					.getParameter("linkForLeftUpLogo");
			if (linkForLeftUpLogo != null
					&& !"".equals(linkForLeftUpLogo)) {
		 		if(!linkForLeftUpLogo.startsWith("http://") && !linkForLeftUpLogo.startsWith("https://")) {
		 			linkForLeftUpLogo = "http://" + linkForLeftUpLogo;
		 		}
				
				companyMgr.mergeCompanyConfig(companyID,
						"linkForLeftUpLogo", linkForLeftUpLogo);
			}

			// 网页右上角横幅广告链接地址
			String linkForAdvertisingForRightUp = request
					.getParameter("linkForAdvertisingForRightUp");
			if (linkForAdvertisingForRightUp != null
					&& !"".equals(linkForAdvertisingForRightUp)) {
				companyMgr.mergeCompanyConfig(companyID,
						"linkForAdvertisingForRightUp",
						linkForAdvertisingForRightUp);
			}

			// 网页右下角管理员电子邮箱
			String adminEmail = request
					.getParameter("adminEmail");
			if (adminEmail != null
					&& !"".equals(adminEmail)) {
				companyMgr.mergeCompanyConfig(companyID,
						"adminEmail", adminEmail);
			}

			// 网页右下角管理员电话号码
			String adminTel = request
					.getParameter("adminTel");
			if (adminTel != null
					&& !"".equals(adminTel)) {
				companyMgr.mergeCompanyConfig(companyID,
						"adminTel", adminTel);
			}

			// 运行用户可查看#visibleDirectGroupLevel#级下属的考勤或申请记录
			String visibleDirectGroupLevel = request
					.getParameter("visibleDirectGroupLevel");
			if (visibleDirectGroupLevel != null
					&& !"".equals(visibleDirectGroupLevel)) {
				try {
					Integer.parseInt(visibleDirectGroupLevel);
					companyMgr.mergeCompanyConfig(companyID,
							"visibleDirectGroupLevel", visibleDirectGroupLevel);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}

			}

			// 直接上司在N天后，若未对下属提交的考勤或申请记录进行审批，系统将发电子邮件提醒。
			String dailyTimingReminderApproveInterval = request.getParameter("dailyTimingReminderApproveInterval");
			if (dailyTimingReminderApproveInterval != null && !"".equals(dailyTimingReminderApproveInterval)) {
				try {
					Integer.parseInt(dailyTimingReminderApproveInterval);
					companyMgr.mergeCompanyConfig(companyID, "dailyTimingReminderApproveInterval",
							dailyTimingReminderApproveInterval);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}
			}

			// 直接上司在系统发电子邮件提醒N天后，若仍未进行审批，系统将自动进行批准。
			String autoApproveDays = request
					.getParameter("autoApproveDays");
			if (autoApproveDays != null
					&& !"".equals(autoApproveDays)) {
				try {
					Integer.parseInt(autoApproveDays);
					companyMgr.mergeCompanyConfig(companyID,
							"autoApproveDays", autoApproveDays);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}

			}

			// 用户联系N天未提交考勤记录时，系统将自动发电子邮件提醒。
			String dailyTimingReminderInterval = request
					.getParameter("dailyTimingReminderInterval");
			if (dailyTimingReminderInterval != null
					&& !"".equals(dailyTimingReminderInterval)) {
				try {
					Integer.parseInt(dailyTimingReminderInterval);
					companyMgr.mergeCompanyConfig(companyID,
							"dailyTimingReminderInterval", dailyTimingReminderInterval);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}
			}

			// 公司的考勤周期是自然月吗？
			String dailyTimingInNaturalMonth = request.getParameter("dailyTimingInNaturalMonth");
			if (dailyTimingInNaturalMonth != null && !"".equals(dailyTimingInNaturalMonth)) {
				companyMgr.mergeCompanyConfig(companyID, "dailyTimingInNaturalMonth",
						dailyTimingInNaturalMonth);
			}

			// 公司的每月考勤起始日期是N日
			String dailyTimingStartDatePerMonth = request
					.getParameter("dailyTimingStartDatePerMonth");
			if (dailyTimingStartDatePerMonth != null
					&& !"".equals(dailyTimingStartDatePerMonth)) {
				try {
					Integer.parseInt(dailyTimingStartDatePerMonth);
					companyMgr.mergeCompanyConfig(companyID,
							"dailyTimingStartDatePerMonth", dailyTimingStartDatePerMonth);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}
			}

			// 每月的考勤统计表将发送到下面的电子邮件地址
			String dailyTimingStatisticsEmailAddrs = request
					.getParameter("dailyTimingStatisticsEmailAddrs");
			if (dailyTimingStatisticsEmailAddrs != null
					&& !"".equals(dailyTimingStatisticsEmailAddrs)) {
				companyMgr.mergeCompanyConfig(companyID,
						"dailyTimingStatisticsEmailAddrs",
						dailyTimingStatisticsEmailAddrs);
			}

			// 每月的消费记录对账单将发送到下面的电子邮件地址
			String consumeStatisticsEmailAddrs = request
					.getParameter("consumeStatisticsEmailAddrs");
			if (consumeStatisticsEmailAddrs != null
					&& !"".equals(consumeStatisticsEmailAddrs)) {
				companyMgr.mergeCompanyConfig(companyID,
						"consumeStatisticsEmailAddrs", consumeStatisticsEmailAddrs);
			}

			// 考勤记录保存
			String dailyTimingSavedMonth = request
					.getParameter("dailyTimingSavedMonth");
			if (dailyTimingSavedMonth != null
					&& !"".equals(dailyTimingSavedMonth)) {
				try {
					Integer.parseInt(dailyTimingSavedMonth);
					companyMgr.mergeCompanyConfig(companyID,
							"dailyTimingSavedMonth", dailyTimingSavedMonth);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}
			}

			// 申请记录需保存
			String applicationSavedMonth = request
					.getParameter("applicationSavedMonth");
			if (applicationSavedMonth != null
					&& !"".equals(applicationSavedMonth)) {
				try {
					Integer.parseInt(applicationSavedMonth);
					companyMgr.mergeCompanyConfig(companyID,
							"applicationSavedMonth", applicationSavedMonth);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}
			}

			// 考勤统计需保存
			String dailyTimingStatisticsSavedMonth = request
					.getParameter("dailyTimingStatisticsSavedMonth");
			if (dailyTimingStatisticsSavedMonth != null
					&& !"".equals(dailyTimingStatisticsSavedMonth)) {
				try {
					Integer.parseInt(dailyTimingStatisticsSavedMonth);
					companyMgr.mergeCompanyConfig(companyID,
							"dailyTimingStatisticsSavedMonth",
							dailyTimingStatisticsSavedMonth);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}
			}

			// 消费记录需保存
			String consumeStatisticsSavedMonth = request
					.getParameter("consumeStatisticsSavedMonth");
			if (consumeStatisticsSavedMonth != null
					&& !"".equals(consumeStatisticsSavedMonth)) {
				try {
					Integer.parseInt(consumeStatisticsSavedMonth);
					companyMgr.mergeCompanyConfig(companyID,
							"consumeStatisticsSavedMonth",
							consumeStatisticsSavedMonth);
				} catch (Exception e) {
					logger.error(e);
					throw new SureonException("只能够输入数字");
				}
			}
		}

		CompanyConfigBO companyConfigBO = null;
		companyConfigBO = companyMgr.getCompanyConfig(companyID);
		if (companyConfigBO != null) {
			request.getSession().setAttribute(SureonConstants.SESSION_ATTR_COMPAYCONFIG, companyConfigBO);
		}

		return new ModelAndView("admin/systemSet");
	}

	public CompanyMgr getCompanyMgr() {
		return companyMgr;
	}

	public void setCompanyMgr(CompanyMgr companyMgr) {
		this.companyMgr = companyMgr;
	}

}
